package cn.topevery.garden.dal.dao.event;

import cn.topevery.garden.entity.po.event.EventDelayApply;
import cn.topevery.garden.entity.query.EventDelayApplyQuery;
import cn.topevery.garden.entity.vo.event.EventDelayApplyVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 案件延时申请表
 */
@Repository
public class EventDelayApplyDaoImpl extends BaseDaoImpl<EventDelayApply> implements EventDelayApplyDao {
    @Override
    public <P extends DbCsp> List<EventDelayApply> getSearch(P para) {
        return null;
    }

    @Override
    public boolean canApply(String evtId) {
        String sql = "SELECT apply.* FROM \n" +
                "t_event_delay_apply apply \n" +
                "INNER JOIN t_event_info  evt\n" +
                "WHERE apply.c_evt_id=evt.c_evt_id AND apply.c_is_pass=0 AND apply.c_evt_id=:p_evt_id limit 0,1";
        List<KeyValue<Object>> keyValues = new ArrayList<>();
        keyValues.add(new KeyValue<>("p_evt_id", evtId));
        EventDelayApply value = getDataByCustomize(sql, CommandType.Text, keyValues, EventDelayApply.class);
        return ObjectUtils.isEmpty(value);
    }

    @Override
    public EventDelayApply getEntityByColumnEqValue(KeyValue<?> keyValue) {
        return super.getEntityByColumnEqValue(keyValue);
    }

    @Override
    public DataPager<EventDelayApplyVO> getEventDelayApplyList(EventDelayApplyQuery query) {
        String table = "t_event_delay_apply teda\n" +
                "INNER JOIN t_event_info tei ON tei.c_evt_id = teda.c_evt_id\n" +
                "LEFT JOIN t_section ts ON tei.c_section_id = ts.c_id\n" +
                "LEFT JOIN t_company tc ON tei.c_company_id = tc.c_id\n" +
                "LEFT JOIN t_sys_user tsu ON tsu.c_user_id = teda.c_req_user_id\n" +
                "LEFT JOIN t_sys_user au ON au.c_user_id = teda.c_audit_user_id\n" +
                "LEFT JOIN v_wf_process_info art ON tei.c_wf_inst_id = art.c_workflow_instance_id\n";
        String columns = "teda.*,tei.c_code evtCode,art.c_activity_name delayActName,ts.c_name sectionName,tc.c_name companyName,tsu.c_user_name reqUsername,au.c_user_name auditUsername";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("teda.c_id desc");
        sqlQueryMate.where(true, "teda.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(BooleanUtils.isTrue(query.getHasCheck()), "teda.c_is_pass", ScriptConditionEnum.NotEq, 0)
                .where(StringUtils.isNotEmpty(query.getSectionId()), "tei.c_section_id", ScriptConditionEnum.Eq, query.getSectionId())
                .where(StringUtils.isNotEmpty(query.getActivityIdentifier()), "art.c_activity_identifier", ScriptConditionEnum.Eq, query.getActivityIdentifier())
                .where(StringUtils.isNotEmpty(query.getEvtCode()), "tei.c_code", ScriptConditionEnum.Like, query.getEvtCode())
                .where(query.getIsPass() != null, "teda.c_is_pass", ScriptConditionEnum.Eq, query.getIsPass());
        return getDataPager(sqlQueryMate, query.pageSize, query.pageIndex - 1, EventDelayApplyVO.class);
    }
}
